FieldServiceDatabase.QueryWindow = Ext.extend(Ext.Window, {
    modal: true,
    id: 'query-window',
    layout: 'fit',
    width: 500,
    height: 300,
    closeAction: 'hide',
    plain: true,
    title: 'Query',
    initComponent: function(){
        //Reusable config options here
        /*
         * new invoice window
         */
        var invoice_project_store = new Ext.data.JsonStore({
            proxy: new Ext.data.HttpProxy({
                url: '/projects?node=queryproject',
				method: 'POST'
            })
        
        });
		invoice_project_store.on('exception', function(dp, type, action, options, response){
			Ext.MessageBox.confirm('Error', 'Network Temporarily not available, do you want to try again?', function(btn, text){
				if (btn=='yes') {
					dp.load()
				} 
			});
		})
        var invoice_client_store = new Ext.data.JsonStore({
            proxy: new Ext.data.HttpProxy({
                url: '/projects?node=queryclient'
            })
        });
		invoice_client_store.load();
		invoice_client_store.on('exception', function(dp, type, action, options, response){
			Ext.MessageBox.confirm('Error', 'Network Temporarily not available, do you want to try again?', function(btn, text){
				if (btn=='yes') {
					this.load()
				} 
			}, this);
		})
        
        var invoice_client_combo = new Ext.form.ComboBox({
            store: invoice_client_store,
            displayField: 'client_name',
            typeAhead: false,
            mode: 'remote',
            triggerAction: 'all',
            emptyText: 'Select a client...',
            selectOnFocus: true,
            fieldLabel: 'Client',
            name: 'client_name',
            allowBlank: false
        
        });
        
        invoice_client_combo.on('select', function(combo, record){
            var client_name = invoice_client_combo.getValue();
            invoice_project_store.setBaseParam('client_name', client_name);
			invoice_project_store.load();
			invoice_project_combo.reset();
			
        })
        
        var invoice_project_combo = new Ext.form.ComboBox({
            store: invoice_project_store,
            displayField: 'project_name',
            name: 'project_name',
            typeAhead: false,
            mode: 'remote',
            triggerAction: 'all',
            emptyText: 'Select a project...',
            selectOnFocus: true,
            fieldLabel: 'Project',
            allowBlank: false
        
        });
        invoice_project_combo.on('select', function(combo, record){
        
            invoice_job_number.setValue(record.get('project_number'))
        });
        
        var invoice_job_number = new Ext.form.TextField({
            fieldLabel: 'Job Number',
            name: 'project_number'
           
        
        });
        var queryButton = new Ext.Button({
            text: 'Query',
            handler: function(){
                if (query_form.getForm().isValid()) {
					var values = query_form.getForm().getValues();
					var store = this.store;
					var params = {
						'client_name': values['client_name'],
						'from_dt': values['from_dt'],
						'to_dt': values['to_dt'],
						'project_number': values['project_number'],
						'project_name': values['project_name'],
						'testing_type': values['testing_type'],
						'start': 0,
						'limit': FieldServiceDatabase.PAGESIZE,
						'sort': 'client_name',
						'dir': 'ASC'
						
					}

					store.load({params: params});
					this.hide();
					this.query_form.getForm().reset();
					var testgrid_panel = Ext.getCmp('testgrid-panel');
					var title = String.format('Results for Query of {0} - {1}|Client:{2}|Project:{3}|Project Number:{4}|Testing Type: {5}',
					values['from_dt'], values['to_dt'], values['client_name'],values['project_name'], values['project_number'], values['testing_type'] )
					testgrid_panel.setTitle(title)
					
                }
                else {
                    Ext.MessageBox.alert('Validation Error', 'Fix the errors on the form');
                }
            },
            scope: this
        });
		
        var closeButton = new Ext.Button({
            text: 'Close',
            handler: function(){
                query_form.getForm().reset();
                this.hide()
            },
            scope: this
        });
        var query_form = new Ext.FormPanel({
            labelWidth: 75,
            url: '/invoices?node=insert',
            frame: true,
            width: 350,
            defaults: {
                width: 230
            },
            defaultType: 'textfield',
            items: [new Ext.form.DateField({
                fieldLabel: 'From',
                name: 'from_dt',
                allowBlank: false
            
            }), new Ext.form.DateField({
                fieldLabel: 'To',
                name: 'to_dt',
                allowBlank: false
            }), invoice_client_combo, invoice_project_combo, invoice_job_number, {
                xtype: 'radiogroup',
                columns: 2,
                allowBlank: false,
                fieldLabel: 'Testing Type',
                items: [{
                
                    boxLabel: 'Compaction',
                    name: 'testing_type',
                    inputValue: 'compaction'
                }, {
                    boxLabel: 'Asphalt',
                    name: 'testing_type',
                    inputValue: 'asphalt'
                }]
            }],
            buttons: [queryButton, closeButton]
        
        });
		
		this.query_form = query_form;
        
        
        Ext.apply(this, {
            items: query_form
        });
        
        FieldServiceDatabase.QueryWindow.superclass.initComponent.call(this, arguments);
        
        
    }
    
});



